/**
 *====================================================
 * 文件名称: DeliveryNoticeServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.exception.NotFoundException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.DeliveryNoticeEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.finance.project.entity.lexicon.NoticeStateEnum;
import com.yuexunit.finance.project.entity.lexicon.ProductTypeEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: DeliveryNoticeServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(DeliveryNoticeService.class)
public class DeliveryNoticeServiceImpl implements DeliveryNoticeService {

	@Override
	public Long createDeliveryNotice(DeliveryNoticeEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setNoticeState(NoticeStateEnum.UNCONFIRMED);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setDeliveryNoticeNumber(SettingConstants.PRE_NUMBER_NOTICE_DELIVERY + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getDeliveryNoticeId();
	}

	@Override
	public void renovateDeliveryNotice(DeliveryNoticeEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public void updateDeliveryNotice(DeliveryNoticeEntity entity) {
		DeliveryNoticeEntity persist = inquireDeliveryNotice(entity.getDeliveryNoticeId());
		BeanUtils.copyProperties(entity, persist);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public void disableDeliveryNotice(Long deliveryNoticeId) {
		DeliveryNoticeEntity persist = inquireDeliveryNotice(deliveryNoticeId);
		persist.setEnabled(false);
		persist.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(persist);
	}

	@Override
	public DeliveryNoticeEntity inquireDeliveryNotice(Long deliveryNoticeId) {
		if (deliveryNoticeId == null) {
			return null;
		}
		return PersistUtil.get(DeliveryNoticeEntity.class, deliveryNoticeId);
	}

	@Override
	public DeliveryNoticeEntity inquireDeliveryNoticeDetail(Long deliveryNoticeId) {
		DeliveryNoticeEntity entity = inquireDeliveryNotice(deliveryNoticeId);
		if (entity == null || entity.getEnabled() == false) {
			throw new NotFoundException("发货通知不存在");
		}
		return entity;
	}
	
	@Override
	public PageList<DeliveryNoticeEntity> inquireDeliveryNoticePageLists(List<Long> financeIds, Long financeId,
			String logisticsCompanyName, String financeNumber, Long logisticsCompanyId, Integer noticeState,String bulkstockName,
			PageCriteria page) {
		List<Long> financeIds2 = null;
		if (bulkstockName !=null && !"".equals(bulkstockName)) {// 若有搜索货物名称
			DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
			if (bulkstockName !=null && !"".equals(bulkstockName)) {
				criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName + "%"));
			}
			criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
			financeIds2 = PersistUtil.get(criteria);
			if (financeIds2.isEmpty()) {
				return new PageList<>(page);
			}
		}
		
		DetachedCriteria criteria = DetachedCriteria.forClass(DeliveryNoticeEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if(financeIds2 !=null && financeIds2.size()>0 ){
			criteria.add(Restrictions.in("financeId", financeIds2));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if(financeIds ==null || financeIds.size()==0){
			return new PageList<DeliveryNoticeEntity>(page);
		}
		criteria.add(Restrictions.in("financeId", financeIds));
		if (StringUtils.isNotBlank(logisticsCompanyName)) {
			criteria.add(Restrictions.like("logisticsCompanyName", "%" + logisticsCompanyName + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null && noticeState.intValue() >0 && noticeState !=0) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public PageList<DeliveryNoticeEntity> inquireDeliveryNoticePageList(Long financeId, String logisticsCompanyName, String financeNumber, Long logisticsCompanyId, Integer noticeState, Integer productType, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(DeliveryNoticeEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (StringUtils.isNotBlank(logisticsCompanyName)) {
			criteria.add(Restrictions.like("logisticsCompanyName", "%" + logisticsCompanyName + "%"));
		}
		if (StringUtils.isNotBlank(financeNumber)) {
			criteria.add(Restrictions.like("financeNumber", "%" + financeNumber + "%"));
		}
		if (noticeState != null) {
			criteria.add(Restrictions.eq("noticeState", NoticeStateEnum.valueOf(noticeState)));
		}
		if (productType != null && productType !=0) {
			criteria.add(Restrictions.eq("productType", ProductTypeEnum.valueOf(productType)));
		}
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}
}
